#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  3 23:56:09 2017

@author: claude
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Jul  1 15:27:19 2017

@author: claude
"""
import pymongo
import time
from bson.objectid import ObjectId
import commands 
import os

FILE = "crawl_log.txt"

def get_timestamp_str():
    status, output = commands.getstatusoutput("tail "+FILE+"  -n 1")
    if len(output)<2:
        return output
    else:
        return output[:-1]

def set_timestamp_str():       
    with open(FILE,'w') as f:
        f.write(get_current_timespamp_str()+"\n")

def get_current_timespamp_str():
    a = time.time()
    str_a = hex(int(a))[2:]
    str_a += "00000000000000000"
    return str_a
    
class MongoClint():
    collection_item = 'wxReqTxtMsg'
    
    def __init__(self):
        self.client = pymongo.MongoClient('localhost', 27017)
        self.db = self.client.xiaohongshu           

    def get_cmd(self):
        if len(get_timestamp_str()) ==24:
            oid = ObjectId(get_timestamp_str())
        else:
            oid = ObjectId('000000000000000000000000')
        keyword = []
        for x in self.db[self.collection_item].find({'_id':{"$gt":oid},"FromUserName":"oidwyv18We1-3e0MbrqP3Zf2yhAo","MsgType":"text"}):
            if x['Content'][0]=='$':
                keyword.append(x['Content'][1:])
        for x in self.db[self.collection_item].find({'_id':{"$gt":oid},"FromUserName":"oidwyvxPIaLKv2cUG9oGDEUddu9Q","MsgType":"text"}):
            if x['Content'][0]=='$':
                keyword.append(x['Content'][1:])
        set_timestamp_str()
        return keyword
                
if __name__=="__main__":
    if os.path.isfile(FILE)==False:    
        status, output = commands.getstatusoutput("touch "+FILE)
    mongodb = MongoClint()
    keyword = mongodb.get_cmd()
    for x in keyword:
        word = x.encode('UTF-8')
        status, output = commands.getstatusoutput("python search.py "+word)
        print output
    
            
                